package com.cdeledu.thread3.c7hookexception.uncaughtException;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// 通过execute提交任务给线程池
public class ExecuteDemo {

	public static void main(String[] args) throws Exception {
		ExecutorService es = new ThreadPoolExecutor(5, 5, 0l, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(10), new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread thread = new Thread(r);
				thread.setUncaughtExceptionHandler((t, e) -> {
		            e.printStackTrace(); //如果你把这一行注释掉，这个程序将不会抛出任何异常.
		        });
				System.out.println("create: " + thread.getName());
				return thread;
			}
		});
		es.execute(() -> {
			try {
				System.out.println("exe: " + Thread.currentThread().getName());
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(1 / 0); // 这行会导致报错！
		});
    }

}
